import sys
from PySide6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QComboBox, QHBoxLayout, QVBoxLayout
from PySide6.QtWidgets import QFileDialog
from PySide6.QtGui import QPixmap,QImage,QGuiApplication
from PySide6.QtCore import Qt
from opencv_usage.av_operation import AVOperation

class KNUIWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.show_ui()

    def show_ui(self):
        print("show ui")
        hlayout = QHBoxLayout(self)

        left_hlayout = QVBoxLayout()

        self.button_choose_image = QPushButton("选择图片")
        self.button_choose_image.clicked.connect(self.openFileDialog)
        left_hlayout.addWidget(self.button_choose_image)

        self.label_origin_tips = QLabel()
        self.label_origin_tips.setText("原图")
        self.label_origin_tips.setFixedSize(50, 20)
        left_hlayout.addWidget(self.label_origin_tips)
        self.label_origin_image = QLabel()
        self.label_origin_image.setStyleSheet("QLabel { border: 2px dashed blue; }")
        left_hlayout.addWidget(self.label_origin_image)

        self.button_choose_image_two = QPushButton("选择第二张图片")
        self.button_choose_image_two.clicked.connect(self.openFileDialogTwo)
        left_hlayout.addWidget(self.button_choose_image_two)

        self.label_origin_tips_two = QLabel()
        self.label_origin_tips_two.setText("第二张图")
        self.label_origin_tips_two.setFixedSize(60, 20)
        left_hlayout.addWidget(self.label_origin_tips_two)
        self.label_origin_image_two = QLabel()
        self.label_origin_image_two.setStyleSheet("QLabel { border: 2px dashed blue; }")
        left_hlayout.addWidget(self.label_origin_image_two)

        hlayout.addLayout(left_hlayout)

        # right image

        r_hlayout = QVBoxLayout()

        self.label_result_tips = QLabel()
        self.label_result_tips.setText("处理后图")
        self.label_result_tips.setFixedSize(60, 20)
        r_hlayout.addWidget(self.label_result_tips)
        self.label_result_image = QLabel()
        self.label_result_image.setStyleSheet("QLabel { border: 2px dashed blue; }")
        r_hlayout.addWidget(self.label_result_image)

        hlayout.addLayout(r_hlayout)

        # operation
        op_layout = QHBoxLayout()
        left_hlayout.addLayout(op_layout)

        self.button_rorate = QPushButton("旋转")
        self.button_rorate.clicked.connect(self.rorate_image)
        op_layout.addWidget(self.button_rorate)

        self.button_img_hist_spec = QPushButton("直方图规定化")
        self.button_img_hist_spec.clicked.connect(self.image_hist_spec)
        op_layout.addWidget(self.button_img_hist_spec)

    def image_hist_spec(self):
        av = AVOperation()
        result = av.testHistSpecData(self.filename, self.filename_two)

        qt_image = QImage(result.data, result.shape[1], result.shape[0], QImage.Format_RGB888)
        pixmap = QPixmap.fromImage(qt_image)
        curSize = self.label_result_image.size()
        scaled_pixmap = pixmap.scaled(curSize.width(), curSize.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
        self.label_result_image.setPixmap(scaled_pixmap)

    def rorate_image(self):
        av = AVOperation()
        result = av.rorateImageData(self.filename)
        qt_image = QImage(result.data, result.shape[1], result.shape[0], QImage.Format_RGB888)
        pixmap = QPixmap.fromImage(qt_image)
        curSize = self.label_result_image.size()
        scaled_pixmap = pixmap.scaled(curSize.width(), curSize.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
        self.label_result_image.setPixmap(scaled_pixmap)

    def openFileDialog(self):
        options = QFileDialog.Options()
        file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "",
                                                   "图片文件 (*.png *.jpeg *.jpg *.gif *.bmp);;所有文件 (*)",
                                                   options=options)
        self.filename = file_name
        if file_name:
            # 加载图片
            pixmap = QPixmap(file_name)
            curSize = self.label_origin_image.size()
            print("cur size [%d,%d]" % (curSize.width(), curSize.height()))
            scaled_pixmap = pixmap.scaled(curSize.width(), curSize.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
            # 设置 QLabel 显示图片
            self.label_origin_image.setPixmap(scaled_pixmap)

    def openFileDialogTwo(self):
        options = QFileDialog.Options()
        file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "",
                                                   "图片文件 (*.png *.jpeg *.jpg *.gif *.bmp);;所有文件 (*)",
                                                   options=options)
        self.filename_two = file_name
        if file_name:
            # 加载图片
            pixmap = QPixmap(file_name)
            curSize = self.label_origin_image_two.size()
            print("cur size [%d,%d]" % (curSize.width(), curSize.height()))
            scaled_pixmap = pixmap.scaled(curSize.width(), curSize.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
            # 设置 QLabel 显示图片
            self.label_origin_image_two.setPixmap(scaled_pixmap)


class KNWindowDirect:
    def show_all(self):
        app = QApplication([])
        # 创建主窗口实例
        window = KNUIWindow()
        # 设置窗口标题和大小
        window.setWindowTitle("一个测试窗口")
        winsize = QGuiApplication.primaryScreen().size()
        print("size [%d, %d]" % (winsize.width(), winsize.height()))
        window.resize(winsize.width()/2, winsize.height())
        # 显示窗口
        window.show()
        # 运行应用程序
        sys.exit(app.exec())